package com.wondream.example.io.nio.file.simple;

import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author wangjun
 * @note NioWriteTest
 * @date 2023-11-24  11:44
 */
public class NioCopyTest {
    public static void main(String[] args) throws Exception {
        File file = new File("logs/basic.txt");
        // 1.创建流
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream("logs/basic2.txt");
        // 2.从流中得到一个通道
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();
        // 3.分配缓冲区
        ByteBuffer inBuffer = ByteBuffer.allocate((int) file.length());
        ByteBuffer outBuffer = ByteBuffer.allocate((int) file.length());
        // 4.传输数据
        outChannel.transferFrom(inChannel, 0, file.length());
//        inChannel.transferFrom(outChannel, 0, file.length());
        // 5.关闭io
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}
